package com.hnucm.cooklife.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hnucm.cooklife.R;
import com.hnucm.cooklife.bean.db.Origin;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DishUsageAdapter extends RecyclerView.Adapter<DishUsageAdapter.UsageHolder> {

    private Context mContext;
    private List<Origin> mOriginList = new ArrayList<>();

    public DishUsageAdapter(Context context, List<Origin> originList) {
        mContext = context;
        mOriginList.addAll(originList);
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setOriginList(List<Origin> originList) {
        mOriginList.clear();
        mOriginList.addAll(originList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public UsageHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.detail_usage_item, parent, false);
        return new UsageHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull UsageHolder holder, int position) {
        if(mOriginList != null){
            Origin origin = mOriginList.get(position);
            holder.mOriName.setText(Html.fromHtml(origin.oriname,Html.FROM_HTML_MODE_COMPACT));
            holder.mOriMuch.setText(Html.fromHtml(origin.orimuch,Html.FROM_HTML_MODE_COMPACT));
        }
    }

    @Override
    public int getItemCount() {
        return mOriginList == null ? 0 : mOriginList.size();
    }

    class UsageHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.ori_name)
        TextView mOriName;

        @BindView(R.id.ori_much)
        TextView mOriMuch;

        public UsageHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
